package com.wj.services;

import org.springframework.stereotype.Service;

import java.util.Random;

@Service
public class NumberCodeService {

    private static final Random random = new Random(); // 在类级别复用Random实例
    private static final int MIN_LENGTH = 4;
    private static final int MAX_LENGTH = 10;

    public String generateCode(int length) throws IllegalArgumentException {
        if (length < MIN_LENGTH || MAX_LENGTH > 10){
            throw  new IllegalArgumentException("para error, Length must be between 4 and 10 ");
        }
        int min = (int) Math.pow(10, length - 1); // 最小值
        int max = (int) Math.pow(10, length) - 1; // 最大值
        return String.valueOf(random.nextInt((max - min) + 1) + min);
    }
}
